import QtQuick 1.1
import com.nokia.meego 1.0

Dialog {
    id: itemDatabaseDialog

    property Style platformStyle: QueryDialogStyle {}

    property string titleString
    property string message
    property string textFieldText
    property int groupIndex : 0

    property string acceptButtonText
    property string rejectButtonText

    signal accepted(string name, int groupId)

    function selectAll(){
        textField.forceActiveFocus()
        textField.selectAll()
    }

    function openKeyboard(){
        textField.forceActiveFocus()
    }

    title:
        Item{
        id: rowTitle
        width: parent.width
        height: titleText.height

        anchors.left: parent.left
        anchors.bottom: parent.bottom
        //anchors.bottomMargin: itemDatabaseDialog.platformStyle.buttonsColumnSpacing

        Text{
            id: titleText

            text: titleString
            width: parent.width

            anchors.verticalCenter: parent.verticalCenter

            font.family: itemDatabaseDialog.platformStyle.titleFontFamily
            font.pixelSize: itemDatabaseDialog.platformStyle.titleFontPixelSize
            font.bold:  itemDatabaseDialog.platformStyle.titleFontBold
            font.capitalization: itemDatabaseDialog.platformStyle.titleFontCapitalization
            color: itemDatabaseDialog.platformStyle.titleTextColor

            horizontalAlignment: Text.AlignHCenter
            verticalAlignment:   Text.AlignVCenter
        }
    }

    content:
        Grid{
        id: contentGrid
        rows: 4
        columns: 1

        width: parent.width

        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter

        spacing: itemDatabaseDialog.platformStyle.buttonsColumnSpacing/2

        Text{
            id: messageText

            text: message
            width: parent.width
            height: textField.height

            font.family: itemDatabaseDialog.platformStyle.messageFontFamily
            font.pixelSize: itemDatabaseDialog.platformStyle.messageFontPixelSize
            color: itemDatabaseDialog.platformStyle.messageTextColor

            verticalAlignment:   Text.AlignVCenter
        }

        TextField {
            width: parent.width

            id: textField
            text: textFieldText
        }


        Text{
            id: messageTextGroupSelect

            text: qsTr("Select group:")
            width: parent.width
            height: textField.height

            font.family: itemDatabaseDialog.platformStyle.messageFontFamily
            font.pixelSize: itemDatabaseDialog.platformStyle.messageFontPixelSize
            color: itemDatabaseDialog.platformStyle.messageTextColor

            verticalAlignment:   Text.AlignVCenter
        }

        Button {
            width: parent.width

            id: buttonGrupa
            text: dialogGrupa.model.get(groupIndex).name

            onClicked: dialogGrupa.open()

            ToolIcon {
                id: icon
                anchors {
                    right: parent.right
                    verticalCenter: parent.verticalCenter
                }
                iconId: "icon-m-toolbar-down"
                onClicked: dialogGrupa.open()
            }

            style: ButtonStyle{ horizontalAlignment: Text.AlignLeft }
        }

    }

    buttons:
        //The following code is taken from QueryDialog.qml

        Item {
        id: buttonColFiller
        width: parent.width
        height: childrenRect.height

        anchors.top: parent.top
        anchors.topMargin: -20

        Item {id: dummy; anchors.fill:  parent}


        Grid {
            rows: 2
            columns: 1

            id: buttonGrid
            anchors.top: parent.top
            //anchors.topMargin: itemDatabaseDialog.platformStyle.buttonTopMargin
            spacing: itemDatabaseDialog.platformStyle.buttonsColumnSpacing

            /*height: (acceptButton.text  == "" ? 0 : acceptButton.height)
                    + (rejectButton.text == "" ? 0 : rejectButton.height)
                    + anchors.buttonTopMargin  + spacing
            */
            anchors.horizontalCenter: parent.horizontalCenter

            Button {
                id: acceptButton
                text: acceptButtonText
                onClicked: {
                    console.log("accepted "+textField.text+" "+dialogGrupa.model.get(groupIndex).groupId)
                    accepted(textField.text, dialogGrupa.model.get(groupIndex).groupId)
                }
                visible: text != ""
                __dialogButton: true
                platformStyle: ButtonStyle {inverted: true}
            }
            Button {
                id: rejectButton
                text: rejectButtonText
                onClicked: {
                    itemDatabaseDialog.destroy()
                }
                visible: text != ""
                __dialogButton: true
                platformStyle: ButtonStyle {inverted: true}
            }
        }

    }

    states: [
        State {
            name: "inLandscape"
            when: !appWindow.inPortrait
            PropertyChanges {
                target: buttonGrid
                rows: 1
                columns: 2
                anchors.topMargin: itemDatabaseDialog.platformStyle.buttonTopMargin/2
                height: (acceptButton.text  == "" ? 0 : acceptButton.height)
                        + anchors.buttonTopMargin  //+ spacing
            }
            PropertyChanges {
                target: contentGrid
                rows: 2
                columns: 2
                height: textField.height+messageTextGroupSelect.height
                anchors.verticalCenterOffset: -20
            }
            PropertyChanges {
                target: messageText
                width: messageText.parent.width * 0.25
            }
            PropertyChanges {
                target: textField
                width: textField.parent.width * 0.75
            }
            PropertyChanges {
                target: messageTextGroupSelect
                width: messageTextGroupSelect.parent.width * 0.25
            }
            PropertyChanges {
                target: buttonGrupa
                width: buttonGrupa.parent.width * 0.75
                anchors.topMargin: itemDatabaseDialog.platformStyle.buttonTopMargin/4
            }
            PropertyChanges {
                target: rowTitle
                //anchors.verticalCenterOffset: -20
                //anchors.bottomMargin: 0
                height: 0
                visible: false
            }
        },
        State {
            name: "portrait"
            when: appWindow.inPortrait
            PropertyChanges {
                target: buttonGrid
                rows: 2
                columns: 1
                anchors.topMargin: itemDatabaseDialog.platformStyle.buttonTopMargin/4
                height: (acceptButton.text  == "" ? 0 : acceptButton.height)
                        + (rejectButton.text == "" ? 0 : rejectButton.height)
                        + anchors.buttonTopMargin *2 + spacing
                anchors.verticalCenterOffset: -40
            }
            PropertyChanges {
                target: contentGrid
                rows: 4
                columns: 1
                height: textField.height+messageText.height+messageTextGroupSelect.height*2
                anchors.verticalCenterOffset: -60
            }
            PropertyChanges {
                target: rowTitle
                anchors.bottomMargin: 60
            }
        }
    ]

    MySelectionDialog {
        id: dialogGrupa
        titleText: qsTr("Select group")

        model: modelGroupsWithoutAllGroups

        onAccepted: {
            groupIndex = dialogGrupa.selectedIndex
        }
    }
}
